#ifndef GAME_H
#define GAME_H

#include "glstuff.h"

int texture;
int grass;
int wheat;
int rbcutter1, lbcutter1, ltcutter1, rtcutter1; 
int rbcutter2, lbcutter2, ltcutter2, rtcutter2;
int rbcutter3, lbcutter3, ltcutter3, rtcutter3;
int tank;

typedef struct FIELD {

} Field, *Field_P;

const int User = 1;
const int Comp = 2;
const int AttackRadius = 256;
const int WorldSize = 4096;
const int WorldHalf = 2048;
const int ShotTime = 3000;
const float Speed = 64;
const float RotSpeed = 90;

typedef struct TANK {
    float x, y;
    float angle;
    int type;
    Uint32 last_shot_time;
} Tank, *Tank_P;

Tank user_tank;
float DeltaTime = 0;

int tank_move()
{
    float dist, dx, dy;
    dist = DeltaTime * Speed / 1000;
    dx = dist *
    return 1;
}

int tank_rotate_left()
{
    return 1;
}

int tank_rotate_right()
{
    return 1;
}

int tank_shoot()
{
    return 1;
}

int draw_tank()
{
    return 1;
}

int game_init()
{	   
    float ambient[] = {1, 1, 1, 1}; 
    load_image(&texture, "./data/texture1.tga", Alpha);
    load_image(&grass, "./data/grass.tga", NotAlpha);
    load_image(&wheat, "./data/wheat.tga", NotAlpha);
    load_image(&rbcutter1, "./data/rbcutter1.tga", Alpha);
    load_image(&lbcutter1, "./data/lbcutter1.tga", Alpha);
    load_image(&ltcutter1, "./data/ltcutter1.tga", Alpha);
    load_image(&rtcutter1, "./data/rtcutter1.tga", Alpha);
    load_image(&rbcutter2, "./data/rbcutter2.tga", Alpha);  
    load_image(&lbcutter2, "./data/lbcutter2.tga", Alpha);
    load_image(&ltcutter2, "./data/ltcutter2.tga", Alpha);
    load_image(&rtcutter2, "./data/rtcutter2.tga", Alpha); 
    load_image(&rbcutter3, "./data/rbcutter3.tga", Alpha);  
    load_image(&lbcutter3, "./data/lbcutter3.tga", Alpha);
    load_image(&ltcutter3, "./data/ltcutter3.tga", Alpha);
    load_image(&rtcutter3, "./data/rtcutter3.tga", Alpha); 
    load_image(&tank, "./data/tank.tga", Alpha);    
	glClearColor(0.1, 0.4, 0.3, 0);
	glColor3f(1.0f, 0.0f, 0.0f);   
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);               
	glViewport(0, 0, W, W);
	glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    //l r b t 
    glOrtho(-W/2, W/2, -W/2, W/2, -1000, 1000);                            
    glMatrixMode(GL_MODELVIEW);
	return 1;   
}

int draw_square(int x, int y, int w)
{
    int left = x;
    int right = left + w;
    int top = y;
    int bottom = y - w;
    glBegin(GL_QUADS);
	glTexCoord2i(0, 1);
	glVertex2i(left, top);
	glTexCoord2i(1, 1);
	glVertex2i(right, top);
	glTexCoord2i(1, 0);
	glVertex2i(right, bottom);
	glTexCoord2i(0, 0);
	glVertex2i(left, bottom);
	glEnd();
	return 1;
}

int draw_rect(int x, int y, int w, int h)
{
    int left = x;
    int right = left + w;
    int top = y;
    int bottom = y - h;
    glBegin(GL_QUADS);
	glTexCoord2i(0, 1);
	glVertex2i(left, top);
	glTexCoord2i(1, 1);
	glVertex2i(right, top);
	glTexCoord2i(1, 0);
	glVertex2i(right, bottom);
	glTexCoord2i(0, 0);
	glVertex2i(left, bottom);
	glEnd();
	return 1;
}

int wait()
{
    const Uint32 stable_duration = 1000 / 60; 
    static Uint32 last_time = 0;
    Uint32 time, delta_time, delay;
    
    if(last_time == 0)
        last_time = SDL_GetTicks();
    time = SDL_GetTicks();
    delta_time = time - last_time;
    if(delta_time < stable_duration) {
        delay = stable_duration - delta_time;
        SDL_Delay(delay);
    }
    last_time = SDL_GetTicks();
    return 1;
}

int print_fps()
{
    static Uint32 last_time = 0;
    static Uint32 frame_count = 0;
    static Uint32 frame_time = 0;
    Uint32 time, delta_time;
    int fps;
    char buffer[64];
    
    if(last_time == 0)
        last_time = SDL_GetTicks();
    time = SDL_GetTicks();
    delta_time = time - last_time;
    last_time = time;
    frame_count++;
    frame_time+=delta_time;
    if(frame_time >= 1000) {
        fps = frame_count;
        sprintf(buffer, "Tanks fps %d\n", fps);
        SDL_WM_SetCaption(buffer, 0);
        frame_count = 0;
        frame_time = 0;
    }
    DeltaTime = delta_time;
    return 1;
}

int game_main()
{
    int x, y;
    float ambient[] = {0.15, 0.15, 0.15, 1};
    //static float rot = 0;
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	// eye x y z, center x y z, up x y z
	gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_LIGHTING); 
	glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); 
	glDisable(GL_BLEND);
	glBindTexture(GL_TEXTURE_2D, grass);
	for(x = -W/2; x < W/2; x+=64) {
	   for(y = W/2; y > -W/2; y-=64) {
	       draw_square(x, y, 64);
	   }
	}
	//glBindTexture(GL_TEXTURE_2D, wheat);
	//draw_square(384-W/2, W/2-64, 64);
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	//glBindTexture(GL_TEXTURE_2D, texture);
	//draw_square(0-tsize/2, 0+tsize/2, tsize);
	glBindTexture(GL_TEXTURE_2D, tank);
	draw_square(0-32, 0+32, 64);
	glBindTexture(GL_TEXTURE_2D, rbcutter1);
	draw_square(W/2-128, -W/2+128, 128);
	glBindTexture(GL_TEXTURE_2D, lbcutter1);
	draw_square(-W/2, -W/2+128, 128);
	glBindTexture(GL_TEXTURE_2D, ltcutter1);
	draw_square(-W/2, W/2, 128);
	glBindTexture(GL_TEXTURE_2D, rtcutter1);
	draw_square(W/2-128, W/2, 128);
	glBindTexture(GL_TEXTURE_2D, rbcutter2);
	draw_rect(W/2-256, -W/2+64, 128, 64);
	glBindTexture(GL_TEXTURE_2D, lbcutter2);
	draw_rect(-W/2, -W/2+256, 64, 128);
	glBindTexture(GL_TEXTURE_2D, ltcutter2);
    draw_rect(-W/2+128, W/2, 128, 64);
	glBindTexture(GL_TEXTURE_2D, rtcutter2);
	draw_rect(W/2-64, W/2-128, 64, 128);
	glBindTexture(GL_TEXTURE_2D, rbcutter3);
	draw_rect(W/2-64, -W/2+256, 64, 128);
	glBindTexture(GL_TEXTURE_2D, lbcutter3);
	draw_rect(-W/2+128, -W/2+64, 128, 64);
	glBindTexture(GL_TEXTURE_2D, ltcutter3);
    draw_rect(-W/2, W/2-128, 64, 128);
	glBindTexture(GL_TEXTURE_2D, rtcutter3);
	draw_rect(W/2-256, W/2, 128, 64);
	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	SDL_GL_SwapBuffers();
	gl_print_error();
	wait();
	print_fps();
	return 1;
}

#endif
